package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@Slf4j
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private FollowMapper followMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private UserMapper userMapper;

    /**
     *  已经关注了，取关。没有关注，就关注
     * @param follewId  被关注者Id
     * @param isFollow  是否要进行关注
     * @return
     */
    @Override
    public Result follow(Long follewId, Boolean isFollow) {
        UserDTO user = UserHolder.getUser();
        String key = "follow:"+user.getId().toString();
        if(!isFollow){
            //取关
            Boolean success = followMapper.deleteByFollowUserIdAndUserId(follewId, user.getId());
            if(success){
                redisTemplate.opsForSet().remove(key,follewId.toString());
            }
        }else {
            //关注
            Follow follow = new Follow();
            follow.setFollowUserId(follewId).setUserId(user.getId()).setCreateTime(LocalDateTime.now());
            Boolean success = followMapper.insertRecord(follow);
            if(success){
                redisTemplate.opsForSet().add(key,follewId.toString());
            }
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long follewId) {
        UserDTO user = UserHolder.getUser();
        Integer count = followMapper.countByFollowUserIdAndUserId(follewId, user.getId());
        return Result.ok(count > 0);
    }

    @Override
    public Result followCommon(Long id) {
        String follow1 = "follow:"+id;
        String follow2 = "follow:"+UserHolder.getUser().getId().toString();
        Set<String> intersect = redisTemplate.opsForSet().intersect(follow1, follow2);
        if(CollectionUtil.isEmpty(intersect)){
            return Result.ok(Collections.emptyList());
        }
        List<Long> userIds = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<User> userList = userMapper.queryByIds(userIds);
        List<UserDTO> dtoList = userList.stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());
        return Result.ok(dtoList);
    }
}
